package com.example.foodclient;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import java.util.Timer;
import java.util.TimerTask;

public class SplashActivity extends AppCompatActivity {

    private static final String TAG = "SplashActivity";

    private TextView timeoutTv;
    int timeout = 5;
    private Timer timer;
    private TimerTask timerTask;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        initView();


        timer = new Timer();
        timerTask = new TimerTask() {
            @Override
            public void run() {
                timeout--;
                if (timeout < 1) {
                    timerTask.cancel();
                    timer.cancel();

                    Intent intent = new Intent(SplashActivity.this, MainActivity.class);
                    startActivity(intent);

                }

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        timeoutTv.setText(timeout + "s");
                    }
                });

                Log.e(TAG, "run: " + timeout);
            }
        };

        timer.schedule(timerTask, 1000, 1000);
    }

    private void initView() {
        timeoutTv = findViewById(R.id.timeout_tv);
    }
}
